<?php
/**
 * WooCommerce Product Content Template
 * 产品内容模板，用于产品循环显示
 * 
 * @package WP Bootstrap Woo
 */

defined('ABSPATH') || exit;

global $product;

// 确保产品对象存在
if (empty($product) || !$product->is_visible()) {
    return;
}
?>

<li <?php wc_product_class('product-item', $product); ?>>
    <div class="product-wrap">
        
        <!-- product image -->
        <div class="product-img">
            <a href="<?php the_permalink(); ?>" class="product-link">
                <?php
                /**
                 * Hook: woocommerce_before_shop_loop_item_title
                 *
                 * @hooked woocommerce_show_product_loop_sale_flash - 10
                 * @hooked woocommerce_template_loop_product_thumbnail - 10
                 */
                do_action('woocommerce_before_shop_loop_item_title');
                ?>
            </a>
            
            <!-- product actions -->
            <div class="product-actions">
                <div class="product-action-buttons">
                    
                    <!-- quick view -->
                    <button type="button" class="btn-action btn-quick-view" data-product-id="<?php echo esc_attr($product->get_id()); ?>" title="Quick View">
                        <i class="feather-eye"></i>
                    </button>
                    
                    <!-- wishlist -->
                    <button type="button" class="btn-action btn-wishlist" data-product-id="<?php echo esc_attr($product->get_id()); ?>" title="Add to Wishlist">
                        <i class="feather-heart"></i>
                    </button>
                    
                    <!-- compare -->
                    <button type="button" class="btn-action btn-compare" data-product-id="<?php echo esc_attr($product->get_id()); ?>" title="Compare">
                        <i class="feather-shuffle"></i>
                    </button>
                </div>
                
                <!-- add to cart -->
                <div class="product-add-to-cart">
                    <?php if ($product->is_type('simple') && $product->is_in_stock()) : ?>
                        <a href="<?php echo esc_url($product->add_to_cart_url()); ?>" 
                           data-quantity="1" 
                           data-product_id="<?php echo esc_attr($product->get_id()); ?>" 
                           data-product_sku="<?php echo esc_attr($product->get_sku()); ?>" 
                           class="btn-add-to-cart ajax_add_to_cart">
                            <i class="feather-shopping-cart"></i>
                            <span>Add to Cart</span>
                        </a>
                    <?php else : ?>
                        <a href="<?php the_permalink(); ?>" class="btn-select-options">
                            <i class="feather-eye"></i>
                            <span>Select Options</span>
                        </a>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- sale badge -->
            <?php if ($product->is_on_sale()) : ?>
                <div class="product-badge sale-badge">
                    <?php 
                    if ($product->get_type() === 'variable') {
                        echo '<span>Sale</span>';
                    } else {
                        $regular_price = (float) $product->get_regular_price();
                        $sale_price = (float) $product->get_sale_price();
                        if ($regular_price > 0) {
                            $percentage = round((($regular_price - $sale_price) / $regular_price) * 100);
                            echo '<span>-' . $percentage . '%</span>';
                        } else {
                            echo '<span>Sale</span>';
                        }
                    }
                    ?>
                </div>
            <?php endif; ?>
            
            <!-- out of stock badge -->
            <?php if (!$product->is_in_stock()) : ?>
                <div class="product-badge out-of-stock-badge">
                    <span>Out of Stock</span>
                </div>
            <?php endif; ?>
            
            <!-- new badge -->
            <?php 
            $new_days = 30; // 30天内的产品显示"New"标签
            $created_date = strtotime($product->get_date_created());
            $current_date = time();
            $days_diff = ($current_date - $created_date) / (60 * 60 * 24);
            
            if ($days_diff <= $new_days && !$product->is_on_sale()) : ?>
                <div class="product-badge new-badge">
                    <span>New</span>
                </div>
            <?php endif; ?>
        </div>
        
        <!-- product content -->
        <div class="product-content">
            
            <!-- product category -->
            <div class="product-category">
                <?php
                $categories = get_the_terms($product->get_id(), 'product_cat');
                if ($categories && !is_wp_error($categories)) {
                    $category = reset($categories);
                    echo '<a href="' . get_term_link($category) . '">' . esc_html($category->name) . '</a>';
                }
                ?>
            </div>
            
            <!-- product title -->
            <h6 class="product-title">
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </h6>
            
            <!-- product rating -->
            <div class="product-rating">
                <?php
                /**
                 * Hook: woocommerce_after_shop_loop_item_title
                 *
                 * @hooked woocommerce_template_loop_rating - 5
                 */
                do_action('woocommerce_after_shop_loop_item_title');
                ?>
            </div>
            
            <!-- product price -->
            <div class="product-price">
                <?php
                /**
                 * Hook: woocommerce_after_shop_loop_item_title
                 *
                 * @hooked woocommerce_template_loop_price - 10
                 */
                echo $product->get_price_html();
                ?>
            </div>
            
            <!-- product short description (for list view) -->
            <div class="product-description list-view-only">
                <?php 
                $short_description = $product->get_short_description();
                if ($short_description) {
                    echo '<p>' . wp_trim_words($short_description, 20, '...') . '</p>';
                }
                ?>
            </div>
            
            <!-- product variations (for variable products) -->
            <?php if ($product->is_type('variable')) : ?>
                <div class="product-variations">
                    <?php
                    $attributes = $product->get_variation_attributes();
                    $variation_count = count($product->get_available_variations());
                    
                    if (!empty($attributes)) {
                        $first_attribute = reset($attributes);
                        if (count($first_attribute) <= 5) { // 只显示少于5个选项的属性
                            echo '<div class="variation-swatches">';
                            foreach (array_slice($first_attribute, 0, 5) as $option) {
                                $term = get_term_by('slug', $option, key($attributes));
                                if ($term) {
                                    echo '<span class="variation-swatch" data-value="' . esc_attr($option) . '">' . esc_html($term->name) . '</span>';
                                }
                            }
                            if (count($first_attribute) > 5) {
                                echo '<span class="variation-more">+' . (count($first_attribute) - 5) . ' more</span>';
                            }
                            echo '</div>';
                        } else {
                            echo '<div class="variation-count">' . $variation_count . ' variations available</div>';
                        }
                    }
                    ?>
                </div>
            <?php endif; ?>
            
        </div>
    </div>
</li>